package cn.wx.basic.controller;

import cn.wx.basic.exception.BusinessException;
import cn.wx.basic.service.IVerifyCodeService;
import cn.wx.basic.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/*
*  获取图形验证码
*
* */

@RequestMapping("/verifyCode")
@RestController
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService iVerifyCodeService;


    /*
    * 生成图片验证码
    * */
    @GetMapping("/image/{key}")
    public JsonResult getVerifyCode(@PathVariable (value = "key") String key){

        try {
            //获取图形验证码
         String baseStr  =  iVerifyCodeService.getVerifyCode(key);
            return JsonResult.me().setResultObj(baseStr);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙");
        }
    }


    /*
    *   短信验证码
    * */
    @PostMapping("/smsCode")
    public JsonResult sendMobileCode( @RequestBody Map<String,String> map){

        try {
            iVerifyCodeService.sendMobileCode(map);
            return JsonResult.me();
        }catch (BusinessException e){
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }


    /*
    *   绑定验证码
    *  */
    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode( @RequestBody Map<String,String> map){

        try {
            iVerifyCodeService.binderSmsCode(map);
            return JsonResult.me();
        }catch (BusinessException e){
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }


}
